//
//  LPTHomeViewController.m
//  LPTClient
//
//  Created by mac on 2018/12/3.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "LPTHomeViewController.h"
#import "HoneHeadView.h"
#import "HomeTableViewCell.h"
#import "HomeSectionHeadView.h"
#import "DoctorBarCodeViewController.h"
#import "LPTHomeBannerEntity.h"
#import "LPTHomeMsgEntity.h"
#import "LPTTabBarViewController.h"

@interface LPTHomeViewController ()<UITableViewDelegate,UITableViewDataSource,HoneHeadViewDelegate>

@property (strong, nonatomic)HoneHeadView *headView;

@end

@implementation LPTHomeViewController

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender
{
    if ([identifier isEqualToString:@"pushtomessegeVC"]) {
        
        if ([self shouldCertification]) {
            return YES;
        }else{
            return NO;
        }
    }
    
    return YES;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSubViews];
    
    [self requestBanner];
    
    [self requestMsgList];
}

-(void)initSubViews
{
    self.headView = [HoneHeadView headView];
    self.headView.delegate = self;
    self.tableView.tableHeaderView = self.headView;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = 67;
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestMsgList];
        
    }];
    
    self.tableView.mj_header = header;
}

-(void)requestMsgList
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/text_visit_list" withBody:@{@"page":@(1),@"size":@(1000),@"status":@(1)} withMethod:YTKRequestMethodGET];
    [request startRequestCompleteSuccess:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
       
        [self.tableView.mj_header endRefreshing];
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:data[@"visits"]];
        [self.tableView reloadData];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

-(void)requestBanner
{
    LPTBaseRequest *request = [[LPTBaseRequest alloc] initWithApi:@"/api/slide_list" withBody:@{} withMethod:YTKRequestMethodGET];
    [request startRequestWithClass:[LPTHomeBannerListEntity class] completeSuccess:^(__kindof LPTBaseRequest * _Nullable request, LPTHomeBannerListEntity *data, NSInteger code) {
        
        [self.tableView.mj_header endRefreshing];
        
        NSMutableArray *imgList = [NSMutableArray array];
        for (LPTHomeBannerEntity *entity in data.slides) {
            
            NSString *url = [NSString stringWithFormat:@"%@/%@",BASE_URL,entity.image.url];
            [imgList addObject:url];
        }
        
        [self.headView refreshWithImageList:imgList];
        
    } completeFailed:^(__kindof LPTBaseRequest * _Nullable request, id  _Nonnull data, NSInteger code) {
        
    }];
}

#pragma mark - UITableViewDataSource and Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeTableViewCell" forIndexPath:indexPath];
    [cell refreshWithDic:self.dataSource[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HomeSectionHeadView *headView = [[HomeSectionHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

#pragma mark - HoneHeadViewDelegate
-(void)honeHeadView:(HoneHeadView *)headView didClickAtIndex:(NSInteger)index
{
    switch (index) {
        case 0:
        {
            if ([self shouldCertification]) {
                [self performSegueWithIdentifier:@"pushToAopentretionVC" sender:nil];
            }
        }break;
        case 1:
        {
            if ([self shouldCertification]) {
                [self performSegueWithIdentifier:@"pushToPreManagerVC" sender:nil];
            }
        }break;
        case 2:
        {
            if ([self shouldCertification]) {
                [self performSegueWithIdentifier:@"pushToEarnViewController" sender:nil];
            }
        }break;
        case 3:
        {
            if ([self shouldCertification]) {
                DoctorBarCodeViewController *vc = [DoctorBarCodeViewController instance];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }break;
            
        default:
            break;
    }
}

- (BOOL)shouldCertification
{
    BOOL isCer = [LTPCacheManager shareInstance].isCertified;
    if (!isCer) {
        [((LPTTabBarViewController *)self.tabBarController) showCentifierVC];
    }
    return isCer;
}

@end
